var orderCache = require('../../../utils/mr_xu/apply_order_cache.js')
var common = require('../../../utils/mr_xu/common.js')
var api = require("../../../utils/mr_xu/api_url.js").api
var request = require("../../../utils/mr_xu/request.js")
const app = getApp()
const privince_short = require('../../../utils/city_short.js')

Page({
    /**
     * 页面的初始数据
     */
    data: {
        min_price: '',
        max_price: '',
        privince_short,
        page: 1,
        rows: 30,
        orderString: "createTime.desc",
        items: [],
        status: 11, //11 未完成  12 已完成
        tabs: [{
                label: "未完成",
                status: 11
            },
            {
                label: "已完成",
                status: 12
            }
        ],
        statusConfig: common.statusConfig,
        businessConfig: {
            1: "将车送到指定地点",
            2: "仅找车",
            3: "找车且送到自定地点"
        },
        hasMore: true,
        loading: false,
        timeOrder: '',
        priceOrder: '',
        location: '',
        filterSelectValue: {
            business: '',
            price: '',
            isLimit: '',
            isGps: '',
            province: '',
            city: '',
            leveal: ''
        },
        current_city: '',
        limit_nearby: false,
        filtered: false,
        filterConfig: [{
            type: 'business',
            name: '业务类型',
            items: [{
                    name: '全部',
                    value: ''
                },
                {
                    name: '单找',
                    value: '1'
                },
                {
                    name: '单送',
                    value: '2'
                },
                {
                    name: '找送',
                    value: '3'
                },
            ]
        }, {
            type: 'leveal',
            special: 'price',
            name: '佣金范围',
            items: [{
                    name: '最低价',
                    value: '1',
                    action: 'input_min'
                },
                {
                    name: '最高价',
                    value: '2',
                    action: 'input_max'
                }
            ]
        }, {
            type: 'isLimit',
            name: '限时加价',
            items: [{
                    name: '不限',
                    value: ''
                },
                {
                    name: '非加价业务',
                    value: false
                },
                {
                    name: '加价业务',
                    value: true
                },
            ]
        }, {
            type: 'isGps',
            name: '定位信息',
            items: [{
                    name: '全部',
                    value: ''
                },
                {
                    name: '无定位',
                    value: false
                },
                {
                    name: '有定位',
                    value: true
                },
            ]
        }, {
            type: 'province',
            name: '所属省份',
            items: [{
                name: '川',
                value: '川'
            }]
        }, {
            type: 'city',
            name: '车牌地市',
            items: [{
                    name: 'A',
                    value: 'A'
                },
                {
                    name: 'B',
                    value: 'B'
                },
                {
                    name: 'C',
                    value: 'C'
                },
                {
                    name: 'D',
                    value: 'D'
                },
                {
                    name: 'E',
                    value: 'E'
                },
                {
                    name: 'F',
                    value: 'F'
                },
                {
                    name: 'G',
                    value: 'G'
                },
                {
                    name: 'H',
                    value: 'H'
                },
                {
                    name: 'J',
                    value: 'J'
                },
                {
                    name: 'K',
                    value: 'K'
                },
                {
                    name: 'L',
                    value: 'L'
                },
                {
                    name: 'M',
                    value: 'M'
                },
                {
                    name: 'N',
                    value: 'N'
                },
                {
                    name: 'P',
                    value: 'P'
                },
                {
                    name: 'Q',
                    value: 'Q'
                },
                {
                    name: 'R',
                    value: 'R'
                },
                {
                    name: 'S',
                    value: 'S'
                },
                {
                    name: 'T',
                    value: 'T'
                },
                {
                    name: 'U',
                    value: 'U'
                },
                {
                    name: 'V',
                    value: 'V'
                },
                {
                    name: 'W',
                    value: 'W'
                },
                {
                    name: 'X',
                    value: 'X'
                },
                {
                    name: 'Y',
                    value: 'Y'
                },
                {
                    name: 'Z',
                    value: 'Z'
                },
            ]
        }],
        gotProvince: false,
        showFilterBox: false,
        carNo: '',
        //页面来源，如果是接单方，查看已接的单子，fromway=personal
        fromway: ''
    },
    changeOrder(e) {

        let type = e.currentTarget.dataset.type

        let value = ''
        if (type == 'time') {
            value = this.data.timeOrder
        } else {
            value = this.data.priceOrder
        }

        if (value == '' || value == 'desc') {
            value = 'asc'
        } else {
            value = 'desc'
        }

        if (type == 'time') {
            this.setData({
                timeOrder: value,
            })
        } else {
            this.setData({
                priceOrder: value,
            })
        }

        let allOrder = ''
        if (this.data.timeOrder) {
            allOrder = 'createTime.' + this.data.timeOrder
        }

        if (this.data.priceOrder) {
            allOrder = allOrder == '' ? 'price.' + this.data.priceOrder : 'price.' + this.data.priceOrder + ',createTime.' + this.data.timeOrder
        }

        this.setData({
            orderString: allOrder,
        })
        this.initSearchParam()
        this.getList()

    },
    initSearchParam() {
        this.setData({
            hasMore: true,
            page: 1
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let fromway = options.fromway

        this.setData({
            fromway: fromway ? fromway : ''
        })
        this.getListWhenLoad(options)

        var current_city = ''
        let pcCode = wx.getStorageSync('pcCode')
        if (pcCode) {
            current_city = privince_short.find(o => o.city_code == pcCode)
        }
        this.setData({
            current_city: current_city
        })

    },
    getListWhenLoad(options) {
        if (!options) {
            this.getList()
            return
        }
        let writeCarNo = options.writeCarNo
        let carNo = options.carNo
        if (writeCarNo) {
            this.setData({
                writeCarNo: true,
                carNo: carNo,
                search: carNo
            })
            if (carNo.length == 7 || carNo.length == 8) {
                this.getList()
            }

        } else {
            this.setData({
                writeCarNo: false,
                carNo: '',
                search: ''
            })
            this.initSearchParam()
            this.getList()
        }
    },
    editSearch(e) {
        let value = e.detail.value
        if (value.length > 8) {
            this.setData({
                carNo: value.substring(0, 8)
            })
            return
        }
        this.setData({
            carNo: value
        })
        this.initSearchParam()
        this.getList()
    },
    reset_filter() {
        let filterSelectValue = {
            business: '',
            price: '',
            isLimit: '',
            isGps: '',
            province: '',
            city: ''
        }
        this.setData({
            filterSelectValue: filterSelectValue,
            filtered: false,
            min_price: '',
            max_price: ''
        })
    },
    set_filter() {
        let filterSelectValue = this.data.filterSelectValue

        if (filterSelectValue['province'] == '' && filterSelectValue['city'] != '') {
            app.wxToast.error("请选择所属省份")
            return
        }
        let filtered = false
        for (let i in filterSelectValue) {
            if (filterSelectValue[i]) {
                filtered = true
            }
        }
        this.setData({
            showFilterBox: false,
            filtered: filtered
        })
        this.initSearchParam()
        this.getList()
    },
    chooseFilter(e) {

        let type = e.currentTarget.dataset.type
        let value = e.currentTarget.dataset.value

        let filterSelectValue = this.data.filterSelectValue
        filterSelectValue[type] = filterSelectValue[type] === value ? '' : value

        this.setData({
            filterSelectValue: filterSelectValue
        })

    },
    toFilter() {

        let that = this
        let showFilterBox = !this.data.showFilterBox
        that.setData({
            showFilterBox: showFilterBox,
        })



        if (!that.data.gotProvince) {
            app.http.post(api.get_province, {
                    data: ''
                })
                .then(res => {

                    let items = res.obj
                    if (!items) {
                        app.wxToast.error("省列表获取失败")
                        return
                    }

                    let output = []
                    for (let i in items) {
                        output.push({
                            name: items[i].label,
                            value: items[i].value
                        })
                    }

                    let filterConfig = that.data.filterConfig

                    let firstValue = ''
                    if (filterConfig) {
                        for (let i in filterConfig) {
                            if (filterConfig[i].type == 'province') {
                                filterConfig[i].items = output
                                firstValue = filterConfig[i].items[0].value
                            }
                        }
                        // let filterSelectValue = that.data.filterSelectValue
                        // filterSelectValue.province = firstValue
                        that.setData({
                            filterConfig: filterConfig,
                            gotProvince: true
                            // filterSelectValue: filterSelectValue
                        })
                    }

                })
        }


    },
    filterNearBy() {

        let limit_nearby = !this.data.limit_nearby
        this.setData({
            limit_nearby: limit_nearby
        })

        this.initSearchParam()
        this.getList()
    },
    detail(res) {
        let id = res.currentTarget.dataset.index

        let append = this.data.fromway ? '&fromway=' + this.data.fromway : ''

        wx.navigateTo({
            url: './order_info?id=' + id + append,
        })
    },
    getList(append) {

        let that = this
        if (!that.data.hasMore) {
            console.log('no more')
            return
        }

        if (that.data.loading) {
            return
        }

        that.setData({
            loading: true
        })

        let url = ''
        if (that.data.fromway == 'personal') {
            url = api.agency_own_order_list
        } else {
            url = api.agency_filter_order_list
        }
        let city = that.data.current_city ? that.data.current_city.city_name : ''
        let business = parseInt(that.data.filterSelectValue.business)
        request.post(url, {
            data: JSON.stringify({
                page: that.data.page,
                rows: that.data.rows,
                orderString: that.data.orderString,
                status: that.data.status,
                nearby: that.data.limit_nearby ? city : '',
                business: business ? business : '',
                minPrice: that.data.min_price,
                maxPrice: that.data.max_price,
                isLimit: that.data.filterSelectValue.isLimit,
                isGps: that.data.filterSelectValue.isGps,
                carNo: that.data.carNo ? that.data.carNo : that.data.filterSelectValue.province + that.data.filterSelectValue.city
            })
        }, function(res) {

            if (res.obj.rows.length < that.data.rows) {
                that.setData({
                    hasMore: false
                })
            }

            if (append && res.obj.rows) {
                let oldItems = that.data.items
                for (let i in res.obj.rows) {
                    oldItems.push(res.obj.rows[i])
                }
                that.setData({
                    items: oldItems
                })
            } else {
                that.setData({
                    items: res.obj.rows,
                })
            }

            that.setData({
                loading: false
            })

            wx.stopPullDownRefresh()

        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.initSearchParam()
        this.getList()
    },
    select(res) {
        let that = this
        let index = res.target.dataset.index
        that.setData({
            status: index,
            hasMore: true
        })
        this.initSearchParam()
        this.getList()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

        if (!this.data.hasMore) {
            return
        }

        let page = this.data.page + 1
        console.log('page = ' + page)
        this.setData({
            page: page
        })
        this.getList(true)
    },

    onShow: function(options) {
        if (!this.data.writeCarNo) {
            this.getListWhenLoad()
        }
    },
    take_photo() {
        wx.navigateTo({
            url: './take_photo',
        })
    },
    input_min(e) {

        let min_price = e.detail.value
        this.setData({
            min_price: min_price
        })
    },
    input_max(e) {
        let max_price = e.detail.value
        this.setData({
            max_price: max_price
        })
    }
})